package com.xcity.game.employee.config.impl;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.EnumMap;
import java.util.List;
import java.util.Map;

import com.xcity.game.employee.EmployeeQuality;
import com.xcity.game.employee.config.AbstractConfigure;
import com.xcity.game.employee.config.Configure;
import com.xcity.game.employee.template.EmployeeProduceMoneyByQualityV2Template;
import com.xcity.game.item.ItemEffect;
import com.xcity.game.item.ItemTemplate;

import gnu.trove.map.TIntObjectMap;
import gnu.trove.map.hash.TIntObjectHashMap;
import naga.x.App;
import naga.x.game.template.TemplateService;

public class EmployeeCommonConfigure extends AbstractConfigure {

	protected List<ItemTemplate> expBooks; // 员工经验书
	protected TIntObjectMap<ItemTemplate> skillBooks; // 技能书(key: skillId)
	protected List<ItemTemplate> foods; // 便当（恢复员工精力的物品）
	protected TIntObjectMap<ItemTemplate> employeeItems; // 员工对应的物品(key: employee template id)
	protected List<ItemTemplate> blessExpBooks;
	
	protected Map<EmployeeQuality, EmployeeProduceMoneyByQualityV2Template> quality2produceMoneyTemplate;
	
	public EmployeeCommonConfigure(Configure parent) {
		super(parent);
	}
	
	@Override
	public EmployeeCommonConfigure configure() {
		return this.configureItems().configureProduceMoney();
	}

	private EmployeeCommonConfigure configureItems() {
		skillBooks = new TIntObjectHashMap<>();
		expBooks = new ArrayList<>();
		foods = new ArrayList<>();
		employeeItems = new TIntObjectHashMap<>(32);
		blessExpBooks = new ArrayList<>();
		List<ItemTemplate> items = App.getApp().getServiceManager().get(TemplateService.class).getTemplatesList(ItemTemplate.class);
		for (int i = 0, size = items.size(); i < size; i++) {
			ItemTemplate item = items.get(i);
			switch (item.getEffect()) {
				case ItemEffect.RECOVER_EMPLOYEE_ENERGY:
					foods.add(item);
					break;
				case ItemEffect.EMPLOYEE_SKILL_BOOK:
					skillBooks.put(item.getParameter(0), item);
					break;
				case ItemEffect.ADD_EMPLOYEE_EXP_IMMEDIATELY:
					expBooks.add(item);
					break;
				case ItemEffect.ADD_EMPLOYEE:
					employeeItems.put(item.getParameter(0), item);
					break;
				case ItemEffect.ADD_BLESS_EXP:
					blessExpBooks.add(item);
					break;
			}
		}
		Collections.sort(expBooks, new Comparator<ItemTemplate>() {
			@Override
			public int compare(ItemTemplate item1, ItemTemplate item2) {
				return item1.getParameter(0) - item2.getParameter(0);
			}
		});
		blessExpBooks.sort(new Comparator<ItemTemplate>() {
			@Override
			public int compare(ItemTemplate o1, ItemTemplate o2) {
				return o1.getParameter(0) - o2.getParameter(0);
			}
		});
		return this;
	}
	
	private EmployeeCommonConfigure configureProduceMoney() {
		quality2produceMoneyTemplate = new EnumMap<>(EmployeeQuality.class);
		List<EmployeeProduceMoneyByQualityV2Template> produceMoneyTempList = App.getApp().getServiceManager().get(TemplateService.class).getTemplatesList(EmployeeProduceMoneyByQualityV2Template.class);
		for(int i = 0; i < produceMoneyTempList.size(); i++){
			EmployeeProduceMoneyByQualityV2Template temp = produceMoneyTempList.get(i);
			quality2produceMoneyTemplate.put(temp.getQuality(), temp);
		}
		return this;
	}

	public Map<EmployeeQuality, EmployeeProduceMoneyByQualityV2Template> getQuality2produceMoneyTemplate() {
		return quality2produceMoneyTemplate;
	}
	
	public List<ItemTemplate> getExpBooks() {
		return expBooks;
	}
	
	public List<ItemTemplate> getFoods() {
		return foods;
	}

	public ItemTemplate getSkillBook(int skillId) {
		return skillBooks.get(skillId);
	}
	
	public ItemTemplate getEmployeeItem(int employeeTemplateId) {
		return employeeItems.get(employeeTemplateId);
	}
	
	public List<ItemTemplate> getBlessExpBooks(){
		return blessExpBooks;
	}
}
